package cqut.wys.websocket;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * websocket激活配置
 * @author wuyoushan@zbj.com
 * @date 2017/3/20.
 */
@Configuration
@EnableWebMvc
@EnableWebSocket
@Component
public class WebSocketConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer{


    @Bean
    public SocketHandler socketHandler(){
       return new SocketHandler();
    }

    @Bean
    public TextSocketHandler textSocketHandler(){
        return new TextSocketHandler();
    }

    @Bean
    public WebSocketInterceptor webSocketInterceptor(){
        return new WebSocketInterceptor();
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
            //注册处理拦截器,拦截url为socketServer的请求
            //registry.addHandler(socketHandler(), "/socketServer").addInterceptors(webSocketInterceptor()).setAllowedOrigins("*");

        //使用textSocketHandler处理消息
//        registry.addHandler(textSocketHandler(), "/socketServer").addInterceptors(webSocketInterceptor()).setAllowedOrigins("*");

            //注册SockJs的处理拦截器,拦截url为/sockjs/socketServer的请求
            registry.addHandler(socketHandler(), "/sockjs/socketServer").addInterceptors(webSocketInterceptor()).withSockJS();

        //使用textSocketHandler处理消息
//        registry.addHandler(textSocketHandler(), "/sockjs/socketServer").addInterceptors(webSocketInterceptor()).withSockJS();
    }
}
